import { defineStore } from 'pinia'
import { pinia } from './index'
import { DLog } from '@/utils/log'

interface DockerAPIHook {
  ip: string
  isWindows: boolean
  router: string
}

export const useDockerApiPath = defineStore({
  id: 'api-router',
  persist: {
    storage: sessionStorage
  },
  state: (): DockerAPIHook => ({
    ip: '',
    router: '',
    isWindows: false
  }),
  getters: {
    routerPath(state) {
      if (state.isWindows) {
        return `http://${state.ip}:33000/?token=argon2:$argon2id$v=19$m=10240,t=10,p=8$/N9hQlayzLjrnbsTL6Kk9Q$BCinGiy2biVSkaPua/pPnBAsbWtSa461l04svkoeyFI`
      } else {
        const routerPrefix = 'dockerapi'
        const IPStr = state.ip.split('.')
        return [routerPrefix, IPStr[IPStr.length - 1]].join('-')
      }
    }
  },
  actions: {
    setIP(ip: string) {
      this.$patch({
        ip
      })
    },
    setWindows(windows: boolean) {
      this.$patch({
        isWindows: windows
      })
    },

    removeAll() {
      this.$reset()
    },
    setRouter(router: string) {
      const routerPrefix = 'dockerapi'
      const IPStr = router.split('.')
      const _router = [routerPrefix, IPStr[IPStr.length - 1]].join('-')
      DLog.Info('docker router', _router)
      this.$patch({
        router: _router,
        ip: router
      })
    }
  }
})

export const useDockerApiPathWithout = () => {
  return useDockerApiPath(pinia)
}
